﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CADDemo2025
{
    public static class UmUserMethod
    {
        /// <summary>
        /// 返回指定类型的List<T>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool SelectEntities<T>(out List<T> result, string message = "选择图形") where T : Entity
        {
            result = new List<T>();
            Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptSelectionOptions pso = new PromptSelectionOptions();
            pso.MessageForAdding = "\n" + message;
            PromptSelectionResult psr = editor.GetSelection(pso);
            if (psr.Status == PromptStatus.OK)
            {
                ObjectId[] objectids = psr.Value.GetObjectIds();
                Database database = HostApplicationServices.WorkingDatabase;
                using (Transaction tran = database.TransactionManager.StartTransaction())
                {
                    foreach (var item in objectids)
                    {
                        Entity entity = item.GetObject(OpenMode.ForRead) as Entity;
                        if (entity is T)
                        {
                            result.Add(entity as T);
                        }

                    }
                }
            }
            return result.Count() > 0;
        }
        /// <summary>
        /// 获取一个Entity
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="message">提示词</param>
        /// <param name="entity">传出的Entity</param>
        /// <returns></returns>
        public static bool GetEntity<T>(out T entity,  string message = "选择图形") where T : Entity
        {
            entity = null;
            Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptEntityResult per = editor.GetEntity("\n" + message);
            if (per.Status == PromptStatus.OK)
            {
                ObjectId objectid = per.ObjectId;
                Database database = HostApplicationServices.WorkingDatabase;
                using (Transaction tran = database.TransactionManager.StartTransaction())
                {
                    entity = objectid.GetObject(OpenMode.ForRead) as T;
                    if (entity == null) return false;
                    else return true;
                }
            }
            return false;
        }
        public static List<T> SelectAllEntities<T>() where T : Entity
        {
            List<T> result = new List<T>();
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForRead);
                foreach (ObjectId item in btr)
                {
                    Entity entity = (Entity)item.GetObject(OpenMode.ForRead);
                    if (entity is T)
                    {
                        result.Add(entity as T);
                    }
                }
            }

            return result;
        }
        /// <summary>
        /// 获取用户输入小数
        /// </summary>
        /// <param name="message">提示词</param>
        /// <param name="d">传出的小数</param>
        /// <returns></returns>
        public static bool GetDouble(out double d, string message = "请输入数字", double value = 0)
        {
            d = 0;
            Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptDoubleOptions pdo = new PromptDoubleOptions(message);
            pdo.DefaultValue = value;
            PromptDoubleResult pdr = editor.GetDouble(pdo);
            if (pdr.Status == PromptStatus.OK)
            {
                d = pdr.Value;
                return true;
            }
            return false;
        }
    }
}
